package com.hupu.jrs.ui.pmdetail

import android.app.Activity
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.hupu.jrs.R
import com.hupu.jrs.bean.PmDetail
import com.hupu.jrs.ui.BaseFragment
import com.hupu.jrs.ui.userprofile.UserProfileActivity
import javax.inject.Inject

/**
 * Created by sll on 2016/5/6.
 */
class PmDetailFragment : BaseFragment(), PmDetailContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.refreshLayout)
    internal var refreshLayout: SwipeRefreshLayout? = null
    @BindView(R.id.recyclerView)
    internal var recyclerView: RecyclerView? = null
    @BindView(R.id.etContent)
    internal var etContent: EditText? = null

    @Inject
    internal var mAdapter: PmDetailAdapter? = null
    @Inject
    internal var mPresenter: PmDetailPresenter? = null
    @Inject
    internal var mActivity: Activity? = null

    private var uid: String? = null
    private var blockItem: MenuItem? = null

    override fun initInjector() {
        getComponent(PmDetailComponent::class.java).inject(this)
    }

    override fun initContentView(): Int {
        return R.layout.fragment_pm_detail
    }

    override fun getBundle(bundle: Bundle) {
        uid = bundle.getString("uid")
    }

    override fun initUI(view: View) {
        ButterKnife.bind(this, view)
        setHasOptionsMenu(true)
        val layoutManager = LinearLayoutManager(mActivity!!.applicationContext)
        recyclerView!!.layoutManager = layoutManager
        recyclerView!!.adapter = mAdapter
        refreshLayout!!.setOnRefreshListener(this)
        mPresenter!!.attachView(this)
    }

    override fun initData() {
        mPresenter!!.onPmDetailReceive()
    }

    override fun showLoading() {
        showProgress(true)
    }

    override fun hideLoading() {
        showContent(true)
    }

    override fun isBlock(isBlock: Boolean) {
        if (blockItem != null) {
            blockItem!!.title = if (isBlock) "取消屏蔽" else "屏蔽该用户"
        }
    }

    override fun renderPmDetailList(pmDetails: MutableList<PmDetail>) {
        mAdapter!!.bind(pmDetails)
    }

    override fun scrollTo(position: Int) {
        recyclerView!!.scrollToPosition(position)
    }

    override fun onRefreshCompleted() {
        refreshLayout!!.isRefreshing = false
    }

    override fun onError() {
        showError(true)
    }

    override fun onEmpty() {
        setEmptyText("暂时没有相关短消息")
        showEmpty(true)
    }

    override fun cleanEditText() {
        etContent!!.setText("")
    }

    override fun onRefresh() {
        mPresenter!!.onLoadMore()
    }

    override fun onReloadClicked() {
        mPresenter!!.onReload()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    @OnClick(R.id.btSend)
    internal fun btSendClick() {
        mPresenter!!.send(etContent!!.text.toString())
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_pm_detail, menu)
        blockItem = menu.findItem(R.id.block)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.toUser -> UserProfileActivity.startActivity(this!!.mActivity!!, this!!.uid!!)
            R.id.clear -> mPresenter!!.clear()
            R.id.block -> mPresenter!!.block()
        }
        return super.onOptionsItemSelected(item)
    }

    companion object {

        fun newInstance(uid: String): PmDetailFragment {
            val mFragment = PmDetailFragment()
            val bundle = Bundle()
            bundle.putString("uid", uid)
            mFragment.setArguments(bundle)
            return mFragment
        }
    }
}
